package com.farmer.order.controller;

import com.farmer.common.result.Result;
import com.farmer.order.dto.CreateOrderDTO;
import com.farmer.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理", description = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "用户下单")
    public Result<?> createOrder(@Valid @RequestBody CreateOrderDTO createOrderDTO) {
        log.info("创建订单请求，用户ID：{}", createOrderDTO.getUserId());
        return orderService.createOrder(createOrderDTO);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderId}")
    @Operation(summary = "获取订单详情", description = "根据订单ID获取订单详细信息")
    public Result<?> getOrderById(@PathVariable Long orderId, @RequestParam(required = false) Long userId) {
        return orderService.getOrderById(orderId, userId);
    }

    /**
     * 获取用户订单列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户订单列表", description = "获取指定用户的订单列表")
    public Result<?> getUserOrderList(@PathVariable Long userId,
                                     @RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     @RequestParam(required = false) String status) {
        return orderService.getUserOrderList(userId, pageNum, pageSize, status);
    }

    /**
     * 获取所有订单列表（管理员）
     */
    @GetMapping("/admin/list")
    @Operation(summary = "获取所有订单列表", description = "管理员查看所有订单")
    public Result<?> getAllOrderList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(required = false) String status,
                                    @RequestParam(required = false) String keyword) {
        return orderService.getAllOrderList(pageNum, pageSize, status, keyword);
    }

    /**
     * 更新订单状态
     */
    @PutMapping("/status/{orderId}")
    @Operation(summary = "更新订单状态", description = "管理员更新订单状态")
    public Result<?> updateOrderStatus(@PathVariable Long orderId, @RequestParam String status) {
        log.info("更新订单状态请求，订单ID：{}，状态：{}", orderId, status);
        return orderService.updateOrderStatus(orderId, status);
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{orderId}")
    @Operation(summary = "取消订单", description = "用户取消订单")
    public Result<?> cancelOrder(@PathVariable Long orderId, @RequestParam(required = false) Long userId) {
        log.info("取消订单请求，订单ID：{}，用户ID：{}", orderId, userId);
        return orderService.cancelOrder(orderId, userId);
    }

    /**
     * 确认收货
     */
    @PutMapping("/receive/{orderId}")
    @Operation(summary = "确认收货", description = "用户确认收货")
    public Result<?> confirmReceive(@PathVariable Long orderId, @RequestParam(required = false) Long userId) {
        log.info("确认收货请求，订单ID：{}，用户ID：{}", orderId, userId);
        return orderService.confirmReceive(orderId, userId);
    }

    /**
     * 获取订单统计
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取订单统计", description = "获取订单统计信息")
    public Result<?> getOrderStatistics() {
        return orderService.getOrderStatistics();
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查服务是否正常运行")
    public Result<?> health() {
        return Result.success("订单服务运行正常");
    }
} 